using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace PaperFighter
{
    class NextLevel : GameScreen
    {
        int level, life, current, total, rank, planeSelect;
        float transparence = 0.0f;
        float exitTransparence = 0.0f;
        Texture2D background, plane, blank, nextLevel;
        bool isExit = false;
        bool isNext = false;

        public NextLevel(int currentScore, int totalScore, int restLevel, int currentLevel, int planeChoose)
        {
            level = currentLevel;
            life = restLevel;
            current = currentScore;
            total = totalScore;
            planeSelect = planeChoose;

            rank = (int)((currentScore / (totalScore * 0.8)) * life + 20) / 20;

            if (current == 0)
                rank = 5;

            if (rank > 5)
                rank = 5;
        }

        public override void LoadContent()
        {
            background = ScreenManager.Game.Content.Load<Texture2D>("background_1");
            plane = ScreenManager.Game.Content.Load<Texture2D>("plane_star");
            blank = ScreenManager.Game.Content.Load<Texture2D>("blank");
            nextLevel = ScreenManager.Game.Content.Load<Texture2D>("nextLevel");
            
            base.LoadContent();
        }

        public override void HandleInput(InputState input)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            PlayerIndex playerIndex;
            if (input.IsMenuCancel(null, out playerIndex))
            {
                isExit = true;
            }

            if (!isExit && !isNext)
            {
                if (input.TouchState.Count > 0)
                {
                    int touchPositionY = (int)input.TouchState[0].Position.Y;
                    if (touchPositionY >= 500 && touchPositionY <= 595)
                        isNext = true;
                    if (touchPositionY >= 600 && touchPositionY <= 680)
                        isExit = true;
                }
            }

            base.HandleInput(input);
        }

        public override void Update(Microsoft.Xna.Framework.GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {
            if (!isExit && !isNext)
            {
                if (transparence < 1)
                    transparence += 0.05f;
            }

            if (isExit || isNext)
            {
                if (exitTransparence > 1)
                {
                    ExitScreen();
                    if (isExit)
                        ScreenManager.AddScreen(new Select(), null);
                    if (isNext)
                    {
                        switch (level)
                        {
                            case 1:
                                ScreenManager.AddScreen(new Play2(planeSelect), null);
                                break;
                            case 2:
                                ScreenManager.AddScreen(new Play3(planeSelect), null);
                                break;
                            case 3:
                                ScreenManager.AddScreen(new Play3(planeSelect), null);
                                break;
                        }
                    }
                }

                exitTransparence += 0.05f;
            }
            
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }

        public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
        {
            ScreenManager.SpriteBatch.Begin();
            ScreenManager.SpriteBatch.Draw(background, Vector2.Zero, Color.White);
            ScreenManager.SpriteBatch.Draw(nextLevel, Vector2.Zero, Color.White * transparence);
            ScreenManager.SpriteBatch.Draw(plane, new Vector2(64, 280), Color.White * transparence);

            if (rank > 1)
                ScreenManager.SpriteBatch.Draw(plane, new Vector2(136, 280), Color.White * transparence);
            if (rank > 2)
                ScreenManager.SpriteBatch.Draw(plane, new Vector2(208, 280), Color.White * transparence);
            if (rank > 3)
                ScreenManager.SpriteBatch.Draw(plane, new Vector2(280, 280), Color.White * transparence);
            if (rank > 4)
                ScreenManager.SpriteBatch.Draw(plane, new Vector2(352, 280), Color.White * transparence);

            if (isNext)
                ScreenManager.SpriteBatch.Draw(blank, Vector2.Zero, new Rectangle(0, 0, 480, 800), Color.White * exitTransparence);
            if (isExit)
                ScreenManager.SpriteBatch.Draw(background, Vector2.Zero, new Rectangle(0, 0, 480, 800), Color.White * exitTransparence);
            ScreenManager.SpriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
